package org.nlp2rdf.navigator.client;

import java.util.ArrayList;
import java.util.List;

import org.nlp2rdf.navigator.client.common.StoredConceptDTO;
import org.nlp2rdf.navigator.client.common.RatingDTO;
import org.nlp2rdf.navigator.client.util.ConceptDTOHelper;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HistoryWidget extends Composite{
	
	

	private Application app;
	private Label collapse;
	
	private List<StoredConceptDTO> learningResults = new ArrayList<StoredConceptDTO>();
	private VerticalPanel conceptsGoHere;



	public HistoryWidget(Application application) {
		
		this.app = application;
		
		VerticalPanel mainPanel = new VerticalPanel();
		
		HorizontalPanel titlePanel = new HorizontalPanel();
		titlePanel.addStyleName("module-title");
		
		
		Label title = new Label("History of Concepts");
		title.addStyleName("module-title");

		
		
		mainPanel.add(title);
		
		
		collapse = new Label("show");
		collapse.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent arg0) {
			if(collapse.getText().equals("show")){
				collapse(false);
			}else{
				collapse(true);
			}
				
			}
		});
		
		mainPanel.add(collapse);
		conceptsGoHere = new VerticalPanel();
		conceptsGoHere.setWidth("100%");
		mainPanel.add(conceptsGoHere);
		
		
		
		mainPanel.addStyleName("module-border");
		mainPanel.setWidth("95%");
		mainPanel.addStyleName("greyed");
		draw();
		initWidget(mainPanel);
	}
	
	
	
	private void draw() {
		for (final StoredConceptDTO concept : learningResults) {
			HorizontalPanel conceptPanel = new HorizontalPanel();
			VerticalPanel conceptMetaPanel = new VerticalPanel();
			conceptPanel.setWidth("100%");
			conceptMetaPanel.setWidth("100px");
			conceptMetaPanel.addStyleName("padding-left:10px");
			//XXX TODO 
//			conceptPanel.add(new HTML(LearningResultHelper.getHTML(concept)+""));
			conceptPanel.add(conceptMetaPanel);
			conceptPanel.setCellHorizontalAlignment(conceptMetaPanel, HasHorizontalAlignment.ALIGN_RIGHT);
			//calculate Rating
			double rating = 0;
			
			if(concept.getRatings().size()>0){
				for (RatingDTO ratingdto : concept.getRatings()) {
				rating += ratingdto.getRating();	
				}
				rating = rating /concept.getRatings().size();
			}
			
			//conceptPanel.add(new Rating((int) Math.round(rating),5));
			Hyperlink load = new Hyperlink("load","load_session");
			load.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent arg0) {
					app.displayLearnedConcept(concept);
					
				}
			});
			
			conceptMetaPanel.add(load);
			
			
			
			//conceptPanel.add(new Rating((int) Math.round(rating),5));
			Hyperlink store = new Hyperlink("store","store_session");
			store.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent arg0) {
					new AddLabelAndCommentDialogBox(concept).show();
					
				}
			});
			
			conceptPanel.addStyleName("bottomline");
			conceptMetaPanel.add(store);
			conceptsGoHere.add(conceptPanel);
			
		}
		
	}



	public void collapse(boolean collapse){
		if(collapse){
			conceptsGoHere.setVisible(false);
			this.collapse.setText("show");
		}else{
			conceptsGoHere.setVisible(true);
			this.collapse.setText("hide");
		}
		
	}

	public void add(StoredConceptDTO learn){
		this.learningResults.add(learn);
		draw();
		
	}
	
	private class AddLabelAndCommentDialogBox extends DialogBox{
		TextBox label;
		TextBox comment;
		private StoredConceptDTO lrdto;
	
		public AddLabelAndCommentDialogBox(StoredConceptDTO dto) {
			this.lrdto  =dto;
			VerticalPanel main = new VerticalPanel();
			Label title = new Label("Please enter a label and a comment");
			title.setStyleName("module-title");
			main.add(title);
			HorizontalPanel lpanel = new HorizontalPanel();
			lpanel.add(new Label("label"));
			label = new TextBox();
			lpanel.add(label);
			main.add(lpanel);
			HorizontalPanel cpanel = new HorizontalPanel();
			cpanel.add(new Label("comment"));
			comment = new TextBox();
			cpanel.add(comment);
			main.add(cpanel);
			
			HorizontalPanel buttonPanel = new HorizontalPanel();
			Button saveButton = new Button("save");
			saveButton.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent arg0) {
					app.getWorkerService().saveConcept(lrdto, new AsyncCallback<Void>() {
						
						public void onSuccess(Void arg0) {
							app.notifyStoredConceptWidget();
							
						}
						
						public void onFailure(Throwable arg0) {
							app.reportError(arg0);
							
						}
					});
					
				}
			});
			buttonPanel.add(saveButton);
			
			Button cancelButton = new Button("cancel");
			cancelButton.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent arg0) {
					hide();
					
				}
			});
			buttonPanel.add(cancelButton);
			
			main.add(buttonPanel);
			add(main);
			
		}
		
	}
}



